# output.tcl - text displaying procs
#
# CONTENTS:
#	createoutput name label chid
#		Creates an "output widget bunch", $name shall be
#		a full path of the output bunch, $label the label
#		this output widget is labeled, and chid is the socket
#		ID the output bunch will use. The parent of $name MUST
#		be a [ttk::notebook] widget.
#
#	scrolloutput units
#		Scrolls the current output widget $units units. $units
#		must begin with a prefix indicating is the digit positive
#		or negative. Example: "+4", "-2"
#
#	deleteoutput name
#		Deletes an output widget bunch
#
#	output msg id ?tags?
#		Inputs text to $id.mainbox.output, with optional [text] tags
#
#	errmsg|warning|notice|infomsg id msg
#		errmsg, warning, notice and infomsg display a special-formatted
#		error message, warning, notice or information message, respectively,
#		to $id.msg
#
#
# API:
#	$name.mainbox.output is a [text] widget, the main widget of the bunch; however,
#	[output] and friends take $name as an argument and automatically put text to
#	$name.mainbox.output
#
#	$name.topic is a [ttk::label] widget containing the channel topic.
#
#	$name.mainbox.vscroll is a [ttk::scrollbar] which acts as the output's scrollbar
#
#	$name.users is a [listbox] widget containing the userlsit
#
# TODO:
#	- Make $name.users a [ttk::treeview]. Quite a drastic change, so it's been pushed
#	  aside for now.
#
#	- $name.users doesn't sort users as it should, this should be fixed.

proc createoutput {name label chid} {
	global FONT
	global CHANS_$chid
	
	regexp {(.+)\.[^\.]+} $name -> parent
	ttk::frame $name
	ttk::label $name.topic -anchor w
	ttk::frame $name.mainbox
	ttk::scrollbar $name.mainbox.vscroll -orient vertical -command [list $name.mainbox.output yview]
	text $name.mainbox.output \
		-yscrollcommand [list $name.mainbox.vscroll set] \
		-font $FONT -background white -height 30 -wrap word
	
	listbox $name.users -width 20 -font $FONT -background white
	ttk::scrollbar $name.userscroll -orient vertical -command $name.mainbox.output
	
	grid columnconfigure	$name 0 -weight 1 
	grid rowconfigure	$name 1 -weight 1 
	grid columnconfigure	$name.mainbox 0 -weight 1 
	grid rowconfigure	$name.mainbox 0 -weight 1 
	
	grid $name.topic		-row 0	-column 0 -sticky ew
	grid $name.mainbox		-row 1	-column 0 -sticky nsew
	grid $name.mainbox.output	-row 0	-column 0 -sticky nsew -in $name.mainbox
	grid $name.mainbox.vscroll	-row 0	-column 1 -sticky nsew -in $name.mainbox
	grid $name.users		-row 1	-column 1 -sticky nsew
	
	$name.mainbox.output tag config bold -font [linsert [$name.mainbox.output cget -font] end bold]
	$name.mainbox.output tag config italic -font [linsert [$name.mainbox.output cget -font] end italic]
	$name.mainbox.output tag config uline -underline 1
	
	set bg "bg"
	foreach color {red darkred blue darkgreen green \
		darkmagenta magenta gray orange grey39 \
		purple darkgreen white darkblue pink yellow \
		aquamarine DeepSkyBlue black darkgray} {
		$name.mainbox.output tag config $color -foreground $color
		$name.mainbox.output tag config $color$bg -background $color
	}
	
	$parent insert end $name -text "$label"
}

proc scrolloutput {units} {
	global conns n_conns
	
	set server [$n_conns tab [$n_conns select] -text]
	set stream $conns($server)
	global CHANS_$stream

	set currservertab $n_conns.$stream
	set channel [$currservertab tab [$currservertab select] -text]
	
	set view $currservertab.console
	if { $channel != "Console" } {
		set num [set "CHANS_$stream\([string tolower $channel]\)"]
		set view $currservertab.chan$num
	}
	
	$view.mainbox.output yview scroll $units units
}

proc deleteoutput {name} {
	regexp {(.+)\.[^\.]+} $name -> parent
	destroy $name.mainbox.output
	destroy $name.mainbox.vscroll
	destroy $name.mainbox
	destroy $name.users
	$parent forget $name
	destroy $name
	$parent select $parent.console
}

proc output {id msg {tags ""}} {
	set tstamp [clock format [clock seconds] -format "%T"]
	$id.mainbox.output insert end "\[$tstamp\] $msg" $tags
	$id.mainbox.output yview end
}

proc errmsg {id msg} {
	set tstamp [clock format [clock seconds] -format "%T"]
	$id.mainbox.output insert end "\[$tstamp\] \[ERROR\] $msg" {darkred bold}
	$id.mainbox.output yview end
}

proc warning {id msg} {
	set tstamp [clock format [clock seconds] -format "%T"]
	$id.mainbox.output insert end "\[$tstamp\] \[WARNING\] $msg" {red bold}
	$id.mainbox.output yview end
}

proc notice {id msg} {
	set tstamp [clock format [clock seconds] -format "%T"]
	if {[winfo exists $id.mainbox.output] == 0} {
		puts "WARNING! $id.mainbox.output doesn't exist."
	}
	$id.mainbox.output insert end "\[$tstamp\] \[NOTICE\] " {blue bold}
	f_output $id.mainbox.output $msg
	$id.mainbox.output yview end
}

proc infomsg {id msg} {
	set tstamp [clock format [clock seconds] -format "%T"]
	$id.mainbox.output insert end "\[$tstamp\] \[INFO\] $msg" {darkgreen bold}
	$id.mainbox.output yview end
}
